package com.mstore.dao;

import java.util.List;

import com.mstore.pojo.BApplication;

public class BApplicationDao extends SuperDao {

	public int findByAccountIdAndStatuPage(BApplication bApplication,
			String beginTime, String endTime) {
		// 查询所有
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId() );
		
		// 当在时间范围内查询

		if (!beginTime.equals("") && !endTime.equals("")
				&& bApplication.getAuditorPass().equals("4")) {
			/*sql = " from BApplication where accountId="
					+ bApplication.getAccountId()
					+ " and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";
*/			
			sb.append(" and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("'");
		}
		// 当时间范围为空，查询是否审批
		if (!bApplication.getAuditorPass().equals("4") && beginTime.equals("")
				&& endTime.equals("")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + " and auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' order by createTime desc";*/
			
			sb.append(" and auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("'");
		}
		// 通过时间和是否审批来查询
		if (!bApplication.getAuditorPass().equals("4") && !beginTime.equals("")
				&& !endTime.equals("")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + " and  auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";*/
			
			sb.append(" and  auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("' and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("'");

		}
		sb.append(" order by createTime desc");
		List<Object> objects = super.find(sb.toString());
		return objects.size();

	}

	// 根据accountId查询
	public List<Object> findByAccountIdAndStatu(int index,
			BApplication bApplication, String beginTime, String endTime) {
		// 查询所有
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId());
	/*	sb.append("order by createTime desc");*/
		// 当在时间范围内查询

		if (!beginTime.equals("") && !endTime.equals("")
				&& bApplication.getAuditorPass().equals("4")) {
			/*sql = " from BApplication where accountId="
					+ bApplication.getAccountId()
					+ " and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";*/
			
			sb.append(" and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("'");
			/*sb.append("' order by createTime desc");*/
			
		}
		// 当时间范围为空，查询是否审批
		if (!bApplication.getAuditorPass().equals("4") && beginTime.equals("")
				&& endTime.equals("")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + " and auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' order by createTime desc";*/
			
			sb.append(" and auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("' order by createTime desc");

		}
		// 通过时间和是否审批来查询
		if (!bApplication.getAuditorPass().equals("4") && !beginTime.equals("")
				&& !endTime.equals("")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + " and  auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";*/

			sb.append(" and  auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("' and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("' order by createTime desc");
		}
		List<Object> objects = super.findPage(index, sb.toString());
		return objects;

	}

	// 修改申请理由
	public void updateApplicationType(BApplication bApplication) {
		/*String sql = "update  b_application   set  application_type="
				+ bApplication.getApplicationType() + "where id="
				+ bApplication.getId();
		super.executeUpdate(sql);*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("update  b_application   set  application_type=");
		sb.append(bApplication.getApplicationType() );
		sb.append("where id=");
		sb.append(bApplication.getId());
	}

	// 提交申请
	public void saveApplication(BApplication bApplication) {
		save(bApplication);
	}

	// id查询
	public Object auditorSelectById(BApplication bApplication) {
		String hql = "from BApplication where id=?";
		List<Object> object = super.getObject(hql,
				new Object[] { bApplication.getId() });
		if (object.size() != 0) {
			return object.get(0);
		}
		else {
			return null;
		}
	}

	// 经过审批
	public void updateAuditorType(BApplication bApplication) {
		/*String sql = "update  b_application   set  auditor_id="
				+ bApplication.getAuditorId() + ",  auditor_pass='"
				+ bApplication.getAuditorPass() + "',  auditor_time='"
				+ bApplication.getAuditorTime() + "', auditor_type="
				+ bApplication.getAuditorType() + " , auditor_reason= '"
				+ bApplication.getAuditorReason() + "'  where id="
				+ bApplication.getId();
		super.executeUpdate(sql);*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("update  b_application   set  auditor_id=");
		sb.append(bApplication.getAuditorId());
		sb.append(",  auditor_pass='");
		sb.append(bApplication.getAuditorPass());
		sb.append("',  auditor_time='");
		sb.append(bApplication.getAuditorTime());
		sb.append("', auditor_type=");
		sb.append(bApplication.getAuditorType());
		sb.append(" , auditor_reason= '");
		sb.append(bApplication.getAuditorReason());
		sb.append("'  where id=");
		sb.append(bApplication.getId());
		
		super.executeUpdate(sb.toString());
	}

	public List<Object> getByShopId(int index, BApplication bApplication) {
		// 查询所有
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + "and  shopId="
				+ bApplication.getShopId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId());
		sb.append("and  shopId=");
		sb.append(bApplication.getShopId());
		sb.append("order by createTime desc");
		List<Object> objects = super.findPage(index, sb .toString());
		return objects;
	}

	public int getByShopPage(BApplication bApplication) {
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + "and  shopId="
				+ bApplication.getShopId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId());
		sb.append("and  shopId=");
		sb.append(bApplication.getShopId());
		sb.append("order by createTime desc");
		List<Object> objects = super.find(sb.toString());
		return objects.size();
	}

	public int getByShopIdByTimePage(BApplication bApplication,
			String beginTime, String endTime) {
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + "and  shopId="
				+ bApplication.getShopId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId());
		sb.append("and  shopId=");
		sb.append(bApplication.getShopId());
		

		if (!beginTime.equals("") && !endTime.equals("")
				&& bApplication.getAuditorPass().equals("4")) {
			/*sql = " from BApplication where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and createTime  between  '"
					+ beginTime + "'  and  '" + endTime
					+ "' order by createTime desc";*/
			
			sb.append(" and createTime  between  '");
			sb.append(beginTime );
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("' order by createTime desc");
		}
		// 当时间范围为空，查询是否审批
		if (beginTime.equals("") && endTime.equals("")
				&& !bApplication.getAuditorPass().equals("4")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' order by createTime desc";*/
			
			sb.append(" and auditorPass='");
			sb.append(bApplication.getAuditorPass());
		}
		// 通过时间和是否审批来查询
		if (!beginTime.equals("") && !endTime.equals("")
				&& !bApplication.getAuditorPass().equals("4")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and  auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";*/
			
			sb.append(" and  auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("' and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("'");
		}
		sb.append(" order by createTime desc");
		List<Object> objects = super.find(sb.toString());
		return objects.size();
	}

	public List<Object> getByShopIdByTime(int index, BApplication bApplication,
			String beginTime, String endTime) {
		/*String sql = "from BApplication where accountId="
				+ bApplication.getAccountId() + "and  shopId="
				+ bApplication.getShopId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId=");
		sb.append(bApplication.getAccountId());
		sb.append("and  shopId=");
		sb.append(bApplication.getShopId());

		if (!beginTime.equals("") && !endTime.equals("")
				&& bApplication.getAuditorPass().equals("4")) {
			/*sql = " from BApplication where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and createTime  between  '"
					+ beginTime + "'  and  '" + endTime
					+ "' order by createTime desc";*/
			
			sb.append(" and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("' order by createTime desc");
		}
		// 当时间范围为空，查询是否审批
		if (!bApplication.getAuditorPass().equals("4") && beginTime.equals("")
				&& endTime.equals("")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' order by createTime desc";*/
			
			sb.append(" and auditorPass='");
			sb.append(bApplication.getAuditorPass());
		}
		// 通过时间和是否审批来查询
		if (!beginTime.equals("") && !endTime.equals("")
				&& !bApplication.getAuditorPass().equals("4")) {
			/*sql = "from BApplication  where accountId="
					+ bApplication.getAccountId() + "and  shopId="
					+ bApplication.getShopId() + " and  auditorPass='"
					+ bApplication.getAuditorPass()
					+ "' and createTime  between  '" + beginTime + "'  and  '"
					+ endTime + "' order by createTime desc";*/
			
			sb.append(" and  auditorPass='");
			sb.append(bApplication.getAuditorPass());
			sb.append("' and createTime  between  '");
			sb.append(beginTime);
			sb.append("'  and  '");
			sb.append(endTime);
			sb.append("'");
		}
		sb.append(" order by createTime desc");
		List<Object> objects = super.findPage(index, sb.toString());
		return objects;
	}

	// 通过id查找
	public Object getById(BApplication bApplication) {
		String hql = "from BApplication where id = ? ";
		List<Object> objects = super.getObject(hql,
				new Object[] { bApplication.getId() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	// 通过account来查找
	// 通过id查找
	public List<Object> getByAccountId(int index, BApplication bApplication) {
		/*String hql = "from BApplication where accountId =  "
				+ bApplication.getAccountId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId =  ");
		sb.append(bApplication.getAccountId());
		sb.append(" order by createTime desc");
		return super.findPage(index, sb.toString());
	}

	public int getByAccountIdPage(BApplication bApplication) {
		/*String hql = "from BApplication where accountId =  "
				+ bApplication.getAccountId() + " order by createTime desc";*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("from BApplication where accountId =  ");
		sb.append(bApplication.getAccountId());
		sb.append(" order by createTime desc");
		List<Object> objects = super.find(sb.toString());
		return objects.size();
	}

	public Object selectByConsumeId(BApplication bApplication) {
		String hql = "FROM BApplication WHERE  logConsumeId=?";
		Object[] values = new Object[] { bApplication.getLogConsumeId() };
		List<Object> list = super.getObject(hql, values);
		if (list.size() != 0) {
			return list.get(0);
		}
		return null;
	}

	public Object selectByBalaceId(BApplication bApplication) {
		String hql = "FROM BApplication WHERE  logBalanceId=?";
		Object[] values = new Object[] { bApplication.getLogBalanceId() };
		List<Object> list = super.getObject(hql, values);
		if (list.size() != 0) {
			return list.get(0);
		}
		return null;
	}
	
	public List<Object> getByMobile(BApplication bApplication){
		/*
		 * String hql =
		 * "FROM BApplication WHERE  logConsumeId="+bApplication.getLogConsumeId
		 * (); return super.find(hql);
		 */
		
		StringBuilder sb=new StringBuilder();
		sb.append("FROM BApplication WHERE  logConsumeId=");
		sb.append(bApplication.getLogConsumeId());
		return super.find(sb.toString());
	}
	
	/**
	 * * 查询是否已申请
	 * * @param logBalanceId
	 */
	public int selectByBalaceIdCount(Long logBalanceId) {
		String hql = "SELECT COUNT(id) FROM BApplication WHERE logBalanceId = ?";
		Object[] values = new Object[] { logBalanceId };
		List<Object> list = super.getObject(hql, values);
		if (list.isEmpty()) {
			return 0;
		}
		return ((Long)list.get(0)).intValue();
	}
	
}
